package com.js.mvpdemo.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

import com.js.mvpdemo.R;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.yokeyword.fragmentation.SupportActivity;

/**
 * 启动页,
 */
public class SplashActivity extends SupportActivity {
   private  Disposable permissionDisposable;

    private String[] permissions = {

            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        permissionDisposable= new RxPermissions(this)
                .requestEachCombined(permissions)
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) throws Exception {
                        if (permission.granted) {
                            //所有权限授予！
                            Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                            SplashActivity.this.startActivity(intent);
                            SplashActivity.this.finish();
                        } else if (permission.shouldShowRequestPermissionRationale) {
                            //至少有一个被拒绝的权限，而不再问任何问题
//                        SplashActivity.this.finish();
                            Toast.makeText(SplashActivity.this, "至少有一个被拒绝的权限", Toast.LENGTH_SHORT).show();
                        } else {
                            //至少有一个被拒绝的权限再问一次
                            //需要去设置
                            // Denied permission with ask never again
                            // Need to go to the settings
                            SplashActivity.this.startActivity(new Intent(Settings.ACTION_SETTINGS));
                        }

                    }
                });

    }

    @Override
    protected void onResume() {
        super.onResume();


    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (!permissionDisposable.isDisposed()) {
            permissionDisposable.isDisposed();
        }
    }
}
